/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sbbs.base.web.primefaces;

import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import org.sbbs.base.dao.BaseDao;
import org.sbbs.genericdao.search.Search;

/**
 *
 * @author Administrator
 * @param <T>
 * @param <ID>
 */
public abstract class BaseLazyView<T, ID extends Serializable> extends BaseView {

    // private BaseLazyDataTable dataTable = new BaseLazyDataTable(true);
    private BaseLazyModel lazyModel = new BaseLazyModel<T, ID>() {
        @Override
        public ID getRowKey(T obj) {
            return getRowObjKey(obj);
        }

        @Override
        public T getRowData(String rowKey) {
            return (T) getDao().findByStringId(rowKey);
        }
    };

    /**
     * @return the dataTable
     */
//    public BaseLazyDataTable getDataTable() {
//        return dataTable;
//    }
//
//    /**
//     * @param dataTable the dataTable to set
//     */
//    public void setDataTable(BaseLazyDataTable dataTable) {
//        this.dataTable = dataTable;
//    }
    /**
     * @return the lazyModel
     */
    public BaseLazyModel getLazyModel() {
        return lazyModel;
    }

    @Override
    public void setDao(BaseDao dao) {
        this.dao = dao;
        this.lazyModel.setDao(this.dao);
    }

    /**
     * @param lazyModel the lazyModel to set
     */
    public void setLazyModel(BaseLazyModel lazyModel) {
        this.lazyModel = lazyModel;

    }

    private List<T> selecteds;

    public List<T> getSelecteds() {
        return selecteds;
    }

    public void setSelecteds(List<T> selecteds) {
        this.selecteds = selecteds;
    }
    private T selected;

    public T getSelected() {
        return selected;
    }

    public void setSelected(T selected) {
        this.selected = selected;
    }

    public abstract ID getRowObjKey(T obj);

    protected void setInitSearch(Search search) {
        this.getLazyModel().setInitSearch(search);
    }

    public void deletes() {
        this.getDao().remove(this.getSelecteds());
        this.showMessageInDialog(new FacesMessage(FacesMessage.SEVERITY_INFO, "删除成功", "删除成功."));
    }

    public void delete() {
        this.getDao().remove(this.getSelected());
        this.showMessageInDialog(new FacesMessage(FacesMessage.SEVERITY_INFO, "删除成功", "删除成功."));
    }
}
